<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	
	protected $logged = 0;
	protected $sub_menus = array();
	function __construct()
	{
		parent::__construct();
		
		$this->load->helper(array("url"));
		$this->load->library(array("session"));
				
		if(!$this->session->userdata('logged')){
			$this->session->set_flashdata("return_url", uri_string());
			$this->logged = 0;
			if(uri_string() != "home" &&  $this->uri->segment(1) == "user"){
				if($this->input->is_ajax_request()){
					$data['redirect'] = site_url('home');
					echo json_encode($data);
					exit();
				}
				else{
					redirect(site_url('home'));
				}
			}
			else{
			
				$active_page = $this->uri->segment(1);
				
				switch ($active_page){
					case "home" : {
						$this->sub_menus = $this->home;
					break;
					}

					case "products" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "history" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "clearing" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "trading_technology" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "cocoa_coffee" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "cotton" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "cocoa_swap" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "electricity" : {
						$this->sub_menus = $this->home;
					break;
					}
					
					case "natural_gas" : {
						$this->sub_menus = $this->home;
					break;
					}
				}
				
				if($this->uri->total_segments() == 0){
					redirect(site_url("home"));
				}
			}
		}
		else{
			$this->logged = 1;
			if ($this->uri->segment(1) != "user"){
				redirect(site_url("user/home"));
			}
			else{
				// TODO determine the active page and load the appropriate submenus...
				
				$active_page = $this->uri->segment(2);
				
				switch ($active_page){
					case "account" : {
						$this->sub_menus = $this->account;
					break;
					}
					
					case "home" : {
						$this->sub_menus = $this->home_user;
					break;
					}

					case "products" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "history" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "clearing" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "trading_technology" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "cocoa_coffee" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "cotton" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "cocoa_swap" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "electricity" : {
						$this->sub_menus = $this->home_user;
					break;
					}
					
					case "natural_gas" : {
						$this->sub_menus = $this->home_user;
					break;
					}
				}
			}
		}
	}
	
	protected function set_logged($value) {
		if(($value == 1) || ($value == 0)){
			$this->logged = $value;
			$this->session->set_userdata("logged", $value);
		}
	}
	
	
	
	protected  $account  = array(
					"Overview" => "user/account",
					"Operations" => "user/account/operations",
					"Transaction History" => "user/account/thistory",
					"Reports" => "user/account/reports",
					"Profile" => "user/account/profile"
	);
	
	
	protected  $home_user  = array(
					"Our products" => "user/products",
					"History" => "user/history",
					"Clearing Worldwide" => "user/clearing",
					"Trading & Techonology" => "user/trading_technology"
	);
	
	protected  $home  = array(
					"Our products" => "products",
					"History" => "history",
					"Clearing Worldwide" => "clearing",
					"Trading & Techonology" => "trading_technology"
	);
	
	
	
	
	/*
	 function verify_access()
	 {
		if($this->session->userdata('role')!="admin")
		{
		$layout['title']="Access restriction";
		$layout['body']="You don't have permision to view this page";
		die($this->load->view('layout', $layout, TRUE));
		}
		}
		*/
}